import {selectFileUrl, selectFirstDocumentId, selectSpaceFileUrl} from '@/api/document'
import {Base64} from 'js-base64'
import {defineStore} from "pinia";


const useDocumentStore = defineStore("document", {
    state: () => ({
        id: '',
        fileUrl: '',
        realUrl: '',
        spaceDocId: '',
    }),
    actions: {
        getLastFileIDAndUrl() {
            return new Promise((resolve, reject) => {
                if (this.id) {
                    resolve(this.id)
                } else {
                    let getFileUrl = documentID => {
                        selectFileUrl(documentID).then(res => {
                            let fileUrl = process.env.VUE_APP_MINIO_NETWORK + res.data.replace('/storage', '')
                            let base64FileUrl = process.env.VUE_APP_PREVIEW_API + '/onlinePreview?url=' + encodeURIComponent(Base64.encode(fileUrl))
                            resolve({id: documentID, url: base64FileUrl})
                            this.id = documentID
                            this.fileUrl = base64FileUrl
                        }).catch(error => {
                            reject(error)
                        })
                    }
                    selectFirstDocumentId().then(res => {
                        let documentId = res.data + ''
                        getFileUrl(documentId)
                    }).catch(error => {
                        reject(error)
                    })
                }
            })
        },

        getLastDocumentID() {
            return new Promise((resolve, reject) => {
                if (this.id) {
                    resolve(this.id)
                } else {
                    selectFirstDocumentId().then(res => {
                        let documentId = res.data + ''
                        resolve(documentId)
                        this.id = documentId
                    }).catch(error => {
                        reject(error)
                    })
                }
            })
        },
        getFileUrl(paramID) { // 获取上次浏览文件的路径，没有的话获取服务器第一份文件的路径
            paramID && (this.id = paramID)
            return new Promise((resolve, reject) => {
                if (!paramID && this.fileUrl) {
                    resolve(this.fileUrl)
                } else {
                    let ID = paramID || this.id
                    selectFileUrl(ID).then(res => {
                        let fileUrl = process.env.VUE_APP_MINIO_NETWORK + res.data.replace('/storage', '')
                        let base64FileUrl = process.env.VUE_APP_PREVIEW_API + '/onlinePreview?url=' + encodeURIComponent(Base64.encode(fileUrl))
                        resolve(base64FileUrl)
                        this.fileUrl = base64FileUrl
                        this.realUrl = res.data
                    }).catch(error => {
                        reject(error)
                    })
                }
            })
        },

        getSpaceFileUrl(paramID) { // 获取上次浏览文件的路径，没有的话获取服务器第一份文件的路径
            paramID && (this.spaceDocId = paramID)
            return new Promise((resolve, reject) => {
                if (!paramID && this.fileUrl) {
                    resolve(this.fileUrl)
                } else {
                    let ID = paramID || this.spaceDocId
                    selectSpaceFileUrl(ID).then(res => {
                        let fileUrl = process.env.VUE_APP_MINIO_NETWORK + res.data.replace('/storage', '')
                        if (res.data.suffixType !== '4') {
                            let base64FileUrl = process.env.VUE_APP_PREVIEW_API + '/onlinePreview?url=' + encodeURIComponent(Base64.encode(fileUrl))
                            resolve(base64FileUrl)
                            this.fileUrl = base64FileUrl
                        }
                    }).catch(error => {
                        reject(error)
                    })
                }
            })
        }
    }
})

export default useDocumentStore